package com.scfw.utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionFactory {
	private static String driver;
	private static String dburl;
	private static String user;
	private static String password;
	
	private static final ConnectionFactory factory = new ConnectionFactory();
	
	private Connection conn;
	
	/**读取配置信息**/
	static {
		Properties prop = new Properties();
		try{
			InputStream in = ConnectionFactory.class.getClassLoader().getResourceAsStream("dbconfig.properties");
			prop.load(in);
		}catch(Exception e){
			System.out.println("数据库连接配置文件读取错误"+e.getMessage());
		}
		driver = prop.getProperty("driver");
		dburl = prop.getProperty("dburl");
		user = prop.getProperty("user");
		password = prop.getProperty("password");
	}
	
	private ConnectionFactory(){}
	
	/**
	 * 获取该类的实例。
	 * @return
	 */
	public static ConnectionFactory getInstance(){
		return factory;
	}
	
	/**
	 * 创建数据库的连接。
	 * @return
	 */
	public Connection makeConnection(){
		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(dburl,user,password);
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void main(String[] args) throws SQLException {
		
		Connection conn = ConnectionFactory.getInstance().makeConnection();
		System.out.println(conn.getAutoCommit());
	}
}
